// pagesFarm/pages/poultrySettle/index.js
var app = getApp(),fetchApi = require("../../../utils/httpPost.js");
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:0,
    adopt:{},
    order:{},
    payPriceFen:0,
    submitState: false,
    remark:'',
    useGoldenMoney:0,
    goldenMoney:0,
    maxGoldenMoney:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.orderId = parseInt(options.id);
      this.onHttpInit();
    }
  },

  /**
   *  获取领养中心列表
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/farm/poultry/order/detail', {
      order_id: that.data.orderId,
    }, function (res) {
      console.log("获取领养订单详情", res)
      if (res.data.code == 0) {
        var order = res.data.data.order;
        var payPrice = order.payPrice;
        that.data.payPriceFen = payPrice * 100; 
        that.setData({
          payPriceFen: that.data.payPriceFen,
          hasData: true,
          order: res.data.data.order,
          adopt:  res.data.data.adopt,
          goldenMoney: res.data.data.goldenMoney,
          maxGoldenMoney: res.data.data.order.maxDiKouGolden,
        })
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false })
    })
  },
  changeCountHandle(e){
    console.log('changeCountHandle',e);
    var count = e.detail;
    if(count > 0){
      this.onHttpChangeBuyCount(count);
    }
  },
  changeGoldenHandle(e){
    console.log('changeCountHandle',e);
    var count = e.detail;
    if(count > 0){
      this.onHttpChangeGoldenCount(count);
    }
  },
  /**
   *  修改购买数量
   */
  onHttpChangeBuyCount: function (buy_count) {
    var that = this;
    fetchApi.httpPost('/farm/poultry/order/changeBuyCount', {
      order_id: that.data.orderId,
      buy_count: buy_count,
    }, function (res) {
      console.log("修改领养数量", res)
      if (res.data.code == 0) {
        var order = res.data.data.order;
        var payPrice = order.payPrice;
        that.data.payPriceFen = payPrice * 100; 
        that.setData({
          order: order,
          payPriceFen:that.data.payPriceFen,
        })
      }else{
        Toast(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false })
    })
  },
  /**
   *  修改金豆数量
   */
  onHttpChangeGoldenCount: function (use_golden) {
    var that = this;
    fetchApi.httpPost('/farm/poultry/order/changeGoldenCount', {
      order_id: that.data.orderId,
      use_golden: use_golden,
    }, function (res) {
      console.log("修改使用抵扣的金豆数量", res)
      if (res.data.code == 0) {
        var order = res.data.data.order;
        var payPrice = order.payPrice;
        that.data.payPriceFen = payPrice * 100; 
        that.setData({
          order: order,
          payPriceFen:that.data.payPriceFen,
        })
      }else{
        Toast(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false })
    })
  },
  onSubmit(){
    var that = this;
    that.data.submitState = true;
    that.setData({
      submitState: true
    })

    fetchApi.httpPost('/farm/poultry/order/requestPay', {
      order_id: that.data.orderId,
      remark:that.data.remark,
      subject:'领养支付'
    }, function (res) {
      console.log("修改领养数量", res)
      if (res.data.code == 0) {
        var wxpayinfo = res.data.data;
        console.log('wxpayinfo',wxpayinfo)
        if(wxpayinfo.result_code == 'SUCCESS'){
          wx.requestPayment({
            timeStamp: wxpayinfo.timeStamp,
            nonceStr: wxpayinfo.nonceStr,
            package: wxpayinfo.package,
            signType: wxpayinfo.signType,
            paySign: wxpayinfo.paySign,
            success (res) { 
              console.log('requestPayment success',res)
              wx.redirectTo({
                url: '../myPoultry/index',
              })
            },
            fail (res) {
              console.log('requestPayment fail',res)
             }
          })
        }else{
          that.setData({
            submitState: true
          })
          Toast.fail(res.data.msg);
        }
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false })
    })
  }
})